package org.biodas.jdas.validation;

import java.util.List;

import org.apache.log4j.Logger;
import org.biodas.jdas.dassources.utils.RegistrySourceAdapter;
import org.biodas.jdas.exceptions.ValidationException;
import org.biodas.jdas.schema.sources.SOURCE;
import org.biodas.jdas.schema.sources.SOURCES;
import org.biodas.jdas.schema.sources.VERSION;


public class RegistrySourceValidator {
	Logger logger=Logger.getLogger(RegistrySourceValidator.class);
	private String message = "";
	
	public RegistrySourceValidator() {
		
	}
	
	public boolean validate(SOURCE source){
		
		if(!validUri(source)|| !hasVersion(source) || !hasCapabilities(source) || !hasCoordinates(source) || !hasMaintainer(source)){
			return false;
		}
		return true;
		
	}
/**
 * Version being the unique unit of the source containing maintainer and capabilities etc
 * need at least one or more of these
 * @param source
 * @return
 */
	public boolean hasVersion(SOURCE source){
		if(source.getVERSION().size()==0){
			message+=" no VERSION found";
			return false;
		}
		return true;
	}
	boolean hasCapabilities(SOURCE source) {
		if(hasVersion(source)){
		if(source.getVERSION().get(0).getCAPABILITY().size()>0){
			return true;
		}
		}
		this.message+=" no capabilities found, ";
		return false;
		
	}

	public String getMessage() {
		return this.message;
	}
	
	public boolean validUri(SOURCE source){
	//uri should contain /das/
		if(source.getUri()==null){
			message+=" no uri detected";
			return false;
		}
	if(source.getUri().contains("/das/")||source.getUri().contains("/das/")||source.getUri().contains("DS_")){
		return true;
	}else{
	
	logger.warn(" uri does not contain /das/ or das1 or DS_ ");
	return true;
	}
	}

	public boolean hasCoordinates(SOURCE source) {
		if(hasVersion(source)){
			VERSION version = source.getVERSION().get(0);
			if(version.getCOORDINATES().size()==0){
				message+= " No coordinates found ";
				return false;
			}
			return true;
		}
		return false;
	}

	public boolean hasMaintainer(SOURCE source) {
		if(source.getMAINTAINER()==null){
			message+=" No Maintainer found ";
			return false;
		}
		if(source.getMAINTAINER().getEmail()==null){
			message+=" no email for maintainer found ";
		return false;
		}
		return true;
	}
	/**
	 * test if a source already exists return the source uri as it exists in the registry else just return ""
	 * @param source
	 * @param regSources
	 * @return regSource.getUri()
	 * @throws ValidationException 
	 */
	public String getRegistryId(SOURCE source, List<SOURCE> regSources) throws ValidationException {
		boolean regPrefix=false;
		//logger.debug("sourceUri in exists="+source.getUri());
		//logger.debug("source description="+source.getDescription());
		RegistrySourceAdapter rA=new RegistrySourceAdapter(source);
		
		
		String	versionUri = rA.getOriginalDataSourceUri();
		
		if(source.getUri().contains("DS_"))regPrefix=true;
		for(SOURCE regSource:regSources){
			//check we have either the url already or the DS_Id if its present
			if(regPrefix){
				if(source.getVERSION().get(0).getUri().equals(regSource.getVERSION().get(0).getUri())){
					return regSource.getUri();
				}
				//search the source uris
			}else{
				//search the query uris
				RegistrySourceAdapter ra=new RegistrySourceAdapter(regSource);
				String regVersionUri="";
				try {
					regVersionUri = ra.getOriginalDataSourceUri();
				} catch (Exception e) {
					//  Auto-generated catch block
					e.printStackTrace();
					continue;
				}
				if(versionUri.equals(regVersionUri)){
					return regSource.getUri();
				}				
			}
			//nickname can already exist but is not allowed to be null, update dev by : ALTER TABLE registry  MODIFY nickname varchar(50)  NOT NULL;
			//if(regSource.getTitle().equals(source.getTitle()))return " nickname the same "+source.getTitle()+ " uri= "+source.getUri();
		}
		return "";
	}
}
